import 'dart:io';
import 'package:date_format/date_format.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:s_mankey/config/net/smonkey_ui_api.dart';
import 'package:s_mankey/config/resource_mananger.dart';
import 'package:s_mankey/config/router_config.dart';
import 'package:s_mankey/config/storage_manager.dart';
import 'package:s_mankey/generated/I18n.dart';
import 'package:s_mankey/model/user_model.dart';
import 'package:s_mankey/provider/provider_widget.dart';
import 'package:s_mankey/service/smonkey_repository.dart';
import 'package:s_mankey/ui/widget/loading_widget.dart';
import 'package:s_mankey/ui/widget/show_toast.dart';
import 'package:s_mankey/view_model/login_vmodel.dart';
import 'package:s_mankey/view_model/my_page_vmodel.dart';

enum PopupMenuItemItemName {
  aspect_ratio,
  border_horizontal,
}

///用户头像信息
class UserAvatarInfo extends StatefulWidget {

  @override
  _UserAvatarInfo createState() => _UserAvatarInfo();
}

class _UserAvatarInfo extends State<UserAvatarInfo> {


  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      width: AppCustomSize.ScreentWidght,
      height: 280,
      child: ProviderWidget<MyPageVModel>(model: MyPageVModel(),onModelReady: (model) => model.loadData(),
        builder: (context, model ,child){
          var loginModel = Provider.of<LoginVModel>(context);
          print('登录了吗${loginModel.isLogin}');
        return (!loginModel.isLogin)? unLoginImg(): (model.userInfo == null)? LoadingWidget() :
      Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Column(
               children: [
                 Container(
                   margin: EdgeInsets.fromLTRB(10,35, 10, 0),
                   child: Row(
                     mainAxisAlignment: MainAxisAlignment.spaceBetween,
                     children: <Widget>[
                     Padding(
                       padding: const EdgeInsets.only(left: 10),
                       child: GestureDetector(onTap: (){
                         Navigator.pushNamed(context, RouterName.checkInfo);
                       },child: Icon(Icons.edit)),
                     ),
//                     rightIcon(),
                   ],),
                 ),

                 Center(
                   child: GestureDetector(onTap: (){
//                     Navigator.pushNamed(context, RouterName.localeWidget);
                   },child:  Container(child: Column(children: [
                     ClipRRect(
                       borderRadius: BorderRadius.circular(50),
                       child: Container(
                         width: 70,
                         height: 70,
                         child:
                         ClipOval(child: Image.network(model.userInfo['header_img'],width: 80,height: 80,fit: BoxFit.fill,),),
                       ),
                     ),
                     Padding(
                       padding: EdgeInsets.only(top: 10),
                       child: Text(
                         model.userInfo['real_name'],
                         style: TextStyle(fontSize: AppFontSize.Size14),
                       ),
                     ),
                   ],),),),
                 ),


                 Container(
                   child: Row(
                     mainAxisAlignment: MainAxisAlignment.center,
                     children: <Widget>[
                       Column(
                         children: [
                           Text(
                             (model.fansDatas['count']).toString(),
                             style: TextStyle(
                                 fontSize: AppFontSize.Size14,
                                 color: Colors.black),
                           ),
                           Text(
                             '${I18n.current.fans}',
                             style: TextStyle(
                                 fontSize: AppFontSize.Size14,
                                 color: Colors.grey),
                           ),
                         ],
                       ),
                       Container(
                         child: Text(''),
                         width: 50,
                       ),
                       Column(
                         children: [
                           Text(
                             (model.fansDatas['has_no_read']).toString(),
                             style: TextStyle(
                                 fontSize: AppFontSize.Size14,
                                 color: Colors.black),
                           ),
                           Text(
                             '${I18n.current.follow}',
                             style: TextStyle(
                                 fontSize: AppFontSize.Size14,
                                 color: Colors.grey),
                           ),
                         ],
                       ),
                     ],
                   ),
                   alignment: Alignment.bottomCenter,
                   padding: EdgeInsets.only(top: 15),
                 ),
               ],
             ),
        ],
      ); },
      ),
    );
  }

  ///右边功能
  Widget rightIcon(){
    return
      Container(
        child: PopupMenuButton(
          icon: Icon(
            Icons.add_circle_outline,
            color: Colors.grey,
          ),
          color: Colors.grey[800],
          offset: Offset(0, 40),
          elevation: 0,
          onSelected: (action) {
            switch (action) {
              case PopupMenuItemItemName.aspect_ratio:
                print('跳到扫一扫');
                Toast.toast(context,msg: "功能暂未开放 ");
//                Navigator.of(context).pushNamed(RouterName.scanPage);
                break;
              case PopupMenuItemItemName.border_horizontal:
                print('跳到我的二维码');
                Toast.toast(context,msg: "功能暂未开放 ");
                break;
            }
          },
          itemBuilder: (BuildContext context) {
            return <PopupMenuItem>[
              PopupMenuItem(
                child: Row(
                  children: <Widget>[
                    Icon(
                      Icons.aspect_ratio,
                      color: Colors.white,
                    ),
                    Text(
                      '  ${I18n.current.scan}',
                      style: TextStyle(color: Colors.white),
                    ),
                  ],
                ),
                value: PopupMenuItemItemName.aspect_ratio,
              ),
              PopupMenuItem(
                child: Row(
                  children: <Widget>[
                    Icon(
                      Icons.border_horizontal,
                      color: Colors.white,
                    ),
                    Text(
                      '  ${I18n.current.qrCode}',
                      style: TextStyle(color: Colors.white),
                    ),
                  ],
                ),
                value: PopupMenuItemItemName.border_horizontal,
              ),
            ];
          },
        ),
        alignment: Alignment.bottomRight,
      );
  }
}

///未登录时显示
class unLoginImg extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GestureDetector(onTap: (){
      Navigator.pushNamed(context, RouterName.loginPage);
    },child: Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
      Icon(Icons.insert_emoticon,size: 50, color: Colors.pinkAccent,),
      Padding(
        padding: const EdgeInsets.only(top: 5),
        child: Text('请先登录 ',style: TextStyle(fontSize: 16),),
      ),
    ],));
  }
}